<?php

/**
 * This will replace 'CURRENT_TIMESTAMP' with '0' on schema.yml and etc...
 *
 */
class fixschemaTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
      // add your own options here
    ));

    $this->namespace        = 'u';
    $this->name             = 'fix-schema';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [fix-schema|INFO] task does things.
Call it with:

  [php symfony u:fix-schema|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();

    sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true));
    
    // add your code here
    set_time_limit(0);
    
    self::step1();
    self::step2();
    self::step3();
    
  }
  
  private static function step1(){
//    echo "\r\nReplace 'CURRENT_TIMESTAMP' with '0' .... \r\n";
    $schema = sfconfig::get('sf_root_dir').'/config/schema.yml';
    $s = str_replace('CURRENT_TIMESTAMP','0',file_get_contents($schema));
    file_put_contents($schema,$s);
  }
  
  private static function step2(){
//    echo "\r\nReplace 'global: { phpName' with 'global2: { phpName' .... \r\n";
    $schema = sfconfig::get('sf_root_dir').'/config/schema.yml';
    $s = str_replace('global: { phpName','global2: { phpName',file_get_contents($schema));
    file_put_contents($schema,$s);
  }

  private static function step3(){
//    echo "\r\nReplace 'continue: { phpName: Continue' with 'continue2: { phpName: Continue' .... \r\n";
    $schema = sfconfig::get('sf_root_dir').'/config/schema.yml';
    $s = str_replace('continue: { phpName: Continue','continue2: { phpName: Continue',file_get_contents($schema));
    file_put_contents($schema,$s);
  }   
    
}
